function promiseAll (array) {
  let result = []
  let length = array.length
  let index = 0
  return new Promise((resolve, reject) => {
    function addData (key, val) {
      index++
      result[key] = val
      if (length === index) {
        resolve(result)
      }
    }
    array.forEach((item, i) => {
      if (item instanceof Promise) {
        item.then((val) => addData(i, val), (reason) => reject(reason))
      } else {
        addData(i, item)
      }
    })
  })
}

let p1 = new Promise(function (resolve, reject) {
  setTimeout(function () {
    resolve(1)
  }, 1000)
})
let p2 = new Promise(function (resolve, reject) {
  setTimeout(function () {
    resolve(2)
  }, 2000)
})
let p3 = new Promise(function (resolve, reject) {
  setTimeout(function () {
    resolve(3)
  }, 3000)
})
promiseAll([p3, p1, p2]).then(res => {
  console.log(res) // [3, 1, 2]
})